﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication3;
namespace bsy
{
    public partial class Customer : Form
    {
        private string customer_id="";

        public Customer()
        {
            InitializeComponent();
        }

        private void Customer_Load(object sender, EventArgs e)
        {
            show_dataGridView1();//显示顾客列表
            show_order(DateTime.Now.ToShortDateString(), DateTime.Now.ToShortDateString(), "");
            init_combox();
            this.dateTimePicker5.Value = DateTime.Now.AddMonths(-1); 
        }

        //初始化combox信息（性别，婚姻）
        private void init_combox()
        {
            Option option = new Option("已婚", "已婚");
            this.marriage_Box.Items.Add(option);
            option = new Option("未婚", "未婚");
            this.marriage_Box.Items.Add(option);
            this.marriage_Box.DisplayMember = "Value";
            this.marriage_Box.ValueMember = "Key";
            this.marriage_Box.SelectedIndex = 1; //默认选择未婚
        }

        //显示顾客列表
        private void show_dataGridView1()
        {
            try
            {
                string selstr = string.Format("select customer_id,number as 顾客编号,name as 顾客名称 from customer");
                enable_edit();
                DataSet ds = new DataSet();
                ds = CommonDB.Query(selstr);
                dataGridView1.DataSource = ds.Tables[0];
                this.dataGridView1.Columns[0].Visible = false; //第一列显示为不可见
                ds.Dispose();
            }
            catch
            {
                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        //显示消费记录
        private void show_order(string t1, string t2, string num)
        {
            try
            {
                string sql = string.Format("select e_name as 员工,price as 价格," +
                     "discount as 折扣,pay as 实收,time as 时间, remark as 备注 from orders,customer where" +
                     " time between '{0}' and '{1}' and issettle='1' and customer.number='{2}' and customer.customer_id =orders.customer_id", t1, t2, num);
                DataSet ds = new DataSet();
                ds = CommonDB.Query(sql);
                dataGridView2.DataSource = ds.Tables[0];
                dataGridView2.Columns[0].Width = 80;
                dataGridView2.Columns[1].Width = 80;
                dataGridView2.Columns[2].Width = 80;
                dataGridView2.Columns[3].Width = 80;
                dataGridView2.Columns[4].Width = 120;
                ds.Dispose();
            }
            catch
            {
                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        //获取文本框的内容
        private string getText()
        {
            string textContent = "";
            if (string.Empty != this.input_textBox.Text)
            {
                textContent = this.input_textBox.Text.ToString();
            }
            return textContent;
        }

        //可编辑
        void enable_edit()
        {
            customername_Box.ReadOnly = false;
            customer_id_Box.ReadOnly = false;
            information_source.ReadOnly = false;
            mobile_Box.ReadOnly = false;
            body_button.ReadOnly = false;
            addrerss_Box.ReadOnly = false;
            marriage_Box.Enabled = true;
            birthdaytextBox.Enabled = true;
            post_code_Box.ReadOnly = false;
            scheme_project.ReadOnly = false;
            businesstextbox.ReadOnly = false;
        }

        //不可编辑
        void unable_edit()
        {
            customername_Box.ReadOnly = true;
            customer_id_Box.ReadOnly = true;
            body_button.ReadOnly = true;
            mobile_Box.ReadOnly = true;
            information_source.ReadOnly = true;
            addrerss_Box.ReadOnly = true;
            marriage_Box.Enabled = false;
            post_code_Box.ReadOnly = true;
            birthdaytextBox.Enabled = false;
            scheme_project.ReadOnly = true;
            businesstextbox.ReadOnly = true;
        }

        //增加新的顾客
        private void add_button_Click_1(object sender, EventArgs e)
        {
            enable_edit();
            customer_id = "";
            string customer_name = "";
            string number = "";
            string mobile = "";
            string address = "";
            string post_code = "";
            string marriage = "";
            string body = "";
            string habit = "";
            string birthday = "";
            string in_fo = "";
            string scheme = "";
            string business = "";

            if (string.Empty == customername_Box.Text)
            {
                MessageBox.Show("姓名不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.Empty == customer_id_Box.Text)
            {
                MessageBox.Show("编号不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            customer_name = customername_Box.Text.Trim();
            number = customer_id_Box.Text.Trim();
            mobile = mobile_Box.Text.Trim();
            address = addrerss_Box.Text.Trim();
            marriage = marriage_Box.Text.Trim();
            birthday = birthdaytextBox.Value.ToShortDateString();
            post_code = post_code_Box.Text.Trim();
            in_fo = information_source.Text.Trim();
            scheme = scheme_project.Text.Trim();
            body = body_button.Text.Trim();
            business = businesstextbox.Text.Trim();
            try
            {
                string selstr = string.Format("select * from customer where customer.number='{0}'", number);
                DataSet ds = new DataSet();
                ds = CommonDB.Query(selstr);
                if (ds.Tables[0].Rows.Count < 1)
                {
                    //定义新增顾客的名称、编号、会员卡号、手机、地址、邮政编码
                    //婚姻、身体状况、生活习惯、生日
                    try
                    {
                        DateTime time = Convert.ToDateTime(birthday);
                    }
                    catch (System.FormatException ex)
                    {
                        MessageBox.Show("生日日期格式不正确", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (MessageBox.Show("是否添加新的顾客", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }
                    string sqlstr1 = string.Format("insert into customer(number,name," +
                        "birthdate,business,marriage,phone,address,post_code," +
                        "habit,info_source,body,scheme)values('{0}','{1}','{2}'," +
                        "'{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')",
                        number, customer_name, birthday, business, marriage, mobile,
                        address, post_code, habit, in_fo, body, scheme);
                    int row = CommonDB.ExecuteSql(sqlstr1);
                    if (row == 1)
                    {
                        MessageBox.Show("添加成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        show_dataGridView1();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("此用户已存在，请使用其他编号", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ds.Dispose();
            }
            catch
            {
                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        //设置为可编辑
        private void button3_Click(object sender, EventArgs e)
        {
            enable_edit();
        }

        //修改顾客表之后保存顾客的信息
        private void save_button_Click(object sender, EventArgs e)
        {
            string customer_name = "";
            string number = "";
            string mobile = "";
            string address = "";
            string post_code = "";
            string marriage = "";
            string body = "";
            string habit = "";
            string birthday = "";
            string in_fo = "";
            string scheme = "";
            string business = "";

            if ("".Equals(customer_id))
            {
                MessageBox.Show("请选择顾客进行修改", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.Empty == customername_Box.Text)
            {
                MessageBox.Show("姓名不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.Empty == customer_id_Box.Text)
            {
                MessageBox.Show("编号不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            customer_name = customername_Box.Text.Trim();
            number = customer_id_Box.Text.Trim(); //编号
            mobile = mobile_Box.Text.Trim();
            address = addrerss_Box.Text.Trim();
            marriage = marriage_Box.Text.Trim();
            birthday = birthdaytextBox.Value.ToShortDateString();
            post_code = post_code_Box.Text.Trim();
            in_fo = information_source.Text.Trim();
            scheme = scheme_project.Text.Trim();
            body = body_button.Text.Trim();
            business = businesstextbox.Text.Trim();
            try
            {
                DateTime time = Convert.ToDateTime(birthday);
            }
            catch
            {
                MessageBox.Show("生日日期格式不正确", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string selstr = string.Format("select * from customer where customer.number='{0}'", number);
            DataSet ds = new DataSet();
            ds = CommonDB.Query(selstr);
            if (ds.Tables[0].Rows.Count>1)
            {
                MessageBox.Show("此用户编号已被使用", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            ds.Dispose();

            string selstr1 = string.Format("update customer set " +
                "number='{0}',name='{1}',birthdate='{2}',business=" +
                "'{3}',marriage='{4}',phone='{5}',address='{6}'," +
                "post_code='{7}',habit='{8}',info_source='{9}'," +
                "body='{10}',scheme='{11}' where customer_id={12}",
                number, customer_name, birthday, business, marriage,
                mobile, address, post_code, habit, in_fo, body, scheme, customer_id);
            int row = CommonDB.ExecuteSql(selstr1);
            if (row > 0)
            {
                customer_id = "";
                MessageBox.Show("修改成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                show_dataGridView1();
                return;
            }
            else
            {
                MessageBox.Show("修改失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }




        //查询顾客消费记录
        private void cost_button_Click(object sender, EventArgs e)
        {
            string time1 = dateTimePicker5.Value.ToShortDateString();
            string time2 = dateTimePicker6.Value.ToShortDateString();
            string customer_num = "";
            if (string.Empty != customer_id_Box.Text)
            {
                customer_num = customer_id_Box.Text.ToString(); //顾客编号
            }
            else
            {
                customer_num = "";
            }
            show_order(time1, time2, customer_num);
        }

        //双击查找顾客信息
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                int row = e.RowIndex;
                if (dataGridView1.Rows.Count > 0)
                {
                    unable_edit(); //设置为不可编辑
                    customer_id = dataGridView1.Rows[row].Cells[0].Value.ToString();
                    DataSet ds = new DataSet();
                    string selstr = string.Format("select * from customer where customer_id={0}", customer_id);
                    ds = CommonDB.Query(selstr);
                    customer_id = ds.Tables[0].Rows[0][0].ToString(); //获取ID
                    customername_Box.Text = ds.Tables[0].Rows[0][2].ToString();
                    customer_id_Box.Text = ds.Tables[0].Rows[0][1].ToString();
                    mobile_Box.Text = ds.Tables[0].Rows[0][6].ToString();
                    //邮政编码的传递、地址、职业、婚姻的传递
                    post_code_Box.Text = ds.Tables[0].Rows[0][8].ToString();
                    addrerss_Box.Text = ds.Tables[0].Rows[0][7].ToString();
                    businesstextbox.Text = ds.Tables[0].Rows[0][4].ToString();
                    marriage_Box.Text = ds.Tables[0].Rows[0][5].ToString();
                    //生日日期、信息来源、和身体状况、护理方案的传递
                    birthdaytextBox.Text = ds.Tables[0].Rows[0][3].ToString();
                    information_source.Text = ds.Tables[0].Rows[0][10].ToString();
                    scheme_project.Text = ds.Tables[0].Rows[0][12].ToString();
                    body_button.Text = ds.Tables[0].Rows[0][11].ToString();
                }
            }
        }

        //查询顾客信息
        private void find_customer_button_Click(object sender, EventArgs e)
        {
            try
            {
                string customer_name = getText();
                DataSet ds = new DataSet();
                string selstr = string.Format("select number as 顾客编号,name as 顾客名称 from customer where name like '%{0}%'", customer_name.ToString().Trim());
                ds = CommonDB.Query(selstr);
                dataGridView1.DataSource = ds.Tables[0];
                //unable_edit();
                ds.Dispose();
            }
            catch
            {
                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        //输入关键字查询时置空
        private void input_textBox_Click(object sender, EventArgs e)
        {
            input_textBox.Text = "";
        }
    }   

}
  




